package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzAgent;
import com.zzedu.domain.ZzAgentGrade;
import com.zzedu.dto.ZzAgentDTO;
import com.zzedu.mapper.ZzAgentMapper;
import com.zzedu.service.IZzAgentGradeService;
import com.zzedu.service.IZzAgentService;
import com.zzedu.system.service.ISysRegionService;
import com.zzedu.vo.ZzAgentVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代理商管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-01-25
 */
@Service
public class ZzAgentServiceImpl extends ServiceImpl<ZzAgentMapper, ZzAgent> implements IZzAgentService {
    @Autowired
    ISysRegionService sysRegionService;

    @Autowired
    IZzAgentGradeService agentGradeService;

    @Override
    public LambdaQueryWrapper<ZzAgent> buildQuery(ZzAgentDTO zzAgent) {
        LambdaQueryWrapper<ZzAgent> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAgent.getContacts()), ZzAgent::getContacts, zzAgent.getContacts());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzAgent.getAgentName()), ZzAgent::getAgentName, zzAgent.getAgentName());
        lambdaQueryWrapper.eq(null != zzAgent.getGradeId(), ZzAgent::getGradeId, zzAgent.getGradeId());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAgent.getTel()), ZzAgent::getTel, zzAgent.getTel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAgent.getProvince()), ZzAgent::getProvince, zzAgent.getProvince());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAgent.getCity()), ZzAgent::getCity, zzAgent.getCity());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAgent.getDistrict()), ZzAgent::getDistrict, zzAgent.getDistrict());
        lambdaQueryWrapper.eq(null != zzAgent.getState(), ZzAgent::getState, zzAgent.getState());
        if (CollectionUtils.isNotEmpty(zzAgent.getGradeIdArray())) {
            lambdaQueryWrapper.in(ZzAgent::getGradeId, zzAgent.getGradeIdArray());
        }
        List<String[]> queryProvinceAndCityAndDistrict = zzAgent.getQueryProvinceAndCityAndDistrict();
        if (CollectionUtils.isNotEmpty(queryProvinceAndCityAndDistrict)) {
            List<String> province = Lists.newArrayList();
            List<String> city = Lists.newArrayList();
            List<String> district = Lists.newArrayList();
            for (String[] provinceAndCityAndDistrict : queryProvinceAndCityAndDistrict) {
                if (null == provinceAndCityAndDistrict) {
                    continue;
                }
                if (StringUtils.isNotBlank(provinceAndCityAndDistrict[0])) {
                    province.add(provinceAndCityAndDistrict[0]);
                }
                if (provinceAndCityAndDistrict.length > 1 && StringUtils.isNotBlank(provinceAndCityAndDistrict[1])) {
                    city.add(provinceAndCityAndDistrict[1]);
                }
                if (provinceAndCityAndDistrict.length > 2 && StringUtils.isNotBlank(provinceAndCityAndDistrict[2])) {
                    district.add(provinceAndCityAndDistrict[2]);
                }
            }
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(province), ZzAgent::getProvince, province);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(city), ZzAgent::getCity, city);
            lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(district), ZzAgent::getDistrict, district);
        }
        lambdaQueryWrapper.orderByDesc(ZzAgent::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public List<ZzAgentVO> convertVO(List<ZzAgent> zzAgentList) {
        List<ZzAgentVO> zzAgentVOList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(zzAgentList)) {
            for (ZzAgent zzAgent : zzAgentList) {
                ZzAgentVO zzAgentVO = new ZzAgentVO();
                BeanUtils.copyBeanProp(zzAgentVO, zzAgent);
                List<String> provinceAndCityAndDistrict = sysRegionService.provinceAndCityAndDistrict(Lists.newArrayList(zzAgent.getProvince(), zzAgent.getCity(), zzAgent.getDistrict()));
                if (CollectionUtils.isNotEmpty(provinceAndCityAndDistrict)) {
                    zzAgentVO.setProvinceAndCityAndDistrict(Joiner.on("/").join(provinceAndCityAndDistrict));
                }
                ZzAgentGrade zzAgentGrade = agentGradeService.getById(zzAgent.getGradeId());
                if (null != zzAgentGrade) {
                    zzAgentVO.setGradeName(zzAgentGrade.getGradeName());
                }
                zzAgentVOList.add(zzAgentVO);
            }
        }
        return zzAgentVOList;
    }

}